<!DOCTYPE html>
<html>
  <head>
	<meta charset="utf-8"> 
    <title>Buddhad(如来)</title>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <link rel="icon" href="favicon.ico">
    <link rel="stylesheet" href="css/bootstrap.min.css"/>
    <link rel="stylesheet" href="css/bootstrap-table.css"/>
    <link rel="stylesheet" href="css/bootstrap-dialog.min.css"/>
    <script src='js/jquery-3.3.1.min.js'></script>
    <script src='js/popper.min.js'></script>
    <script src='js/bootstrap.min.js'></script>
    <script src='js/bootstrap-table.min.js'></script>
    <script src='js/bootstrap-dialog.min.js'></script>
  </head>
  
        
  <script type="text/javascript">
  function formatDate(time) {
    var date = new Date(time);
    var year = date.getFullYear(),
        month = date.getMonth() + 1,//月份是从0开始的
        day = date.getDate(),
        hour = date.getHours(),
        min = date.getMinutes(),
        sec = date.getSeconds();
    var newTime = year + '-' +
        month + '-' +
        day + ' ' +
        hour + ':' +
        min + ':' +
        sec;
    return newTime;
  }

  function isValidDate(date) {
    return date instanceof Date && !isNaN(date.getTime())
  }

  var programs = []

  function refreshDisplay() {
      reformatPrograms( programs );
      $("#programs").bootstrapTable('destroy');
      $("#programs").bootstrapTable({data: programs});
  }

  function downloadLog(name) {
    var $eleForm = $("<form method='get'></form>")
    $eleForm.attr("action","/supervisor/event_log")

    var input1 = document.createElement('input');
    input1.setAttribute('type','hidden');
    input1.setAttribute('name','filename');
    input1.setAttribute('value',name);
    
    $eleForm.get(0).appendChild(input1);

    $(document.body).append($eleForm)
    $eleForm.submit()
  }

  function reformatPrograms( programs ) {
      for( var i in programs ) {
          var LogName = programs[i]['LogName'];
          if( LogName ) {
            action = '<button type="button" class="btn btn-link" style="padding:.175rem .25rem" onclick="downloadLog(\'' + programs[i]['LogName'] + '\');">下载日志</button>';
            programs[i]['action'] = action;
          }
      }
  };

  function confirm_dialog( confirm ) {
        $('#my-modal-title').text(confirm['title'] );
        $('#my-modal-message').text( confirm['message'] );
        $('#my-modal-cancel-btn').text( confirm['cancel-text'] );
        $('#my-modal-confirm-btn').text( confirm['confirm-text'] );
        $('#my-modal-confirm-btn').unbind( 'click' );
        if( confirm.hasOwnProperty( 'cancel-hide') && confirm['cancel-hide'] ) {
            $('#my-modal-cancel-btn').hide();
        } else {
            $('#my-modal-cancel-btn').show();
        }
        $('#my-modal-confirm-btn').click( confirm['confirm-onclick'] );
        $("#myModal").modal('show');

  }

  function list_events() {
    s_date_start = $("#eventTimeStart").val()
    s_date_end = $("#eventTimeEnd").val()
    if(s_date_start.length>0) {
      var date1 = new Date(s_date_start);
      if(isValidDate(date1)) {
        s_date_start = date1.getTime()
      }
    } else {
      // 最近一分钟
      s_date_start = new Date().getTime() - 1000*60
      $("#eventTimeStart").val(formatDate(s_date_start))
    }
    if(s_date_end.length>0) {
      var date1 = new Date(s_date_end);
      if(isValidDate(date1)) {
        s_date_end = date1.getTime()
      }
    }
    
    data = {
      eventTimeStart: s_date_start,
      name: $("#Name").val(),
      type: $("#Type").val(),
      eventTimeEnd: s_date_end
    }
    console.log(data)
    list_events2(data)
  }

  function list_events2(queryParam) {
    $.ajax({
      type: "GET",
      url: "/supervisor/events",
      data: queryParam,
      dataType: "json",
      success: function( data, status, jqXHR ) {
        programs = data;
        for(var i=0; i<programs.length; i++) {
          programs[i].EventTime = formatDate(programs[i].EventTime)
          programs[i].Type = parseType(programs[i].Type)
        }
        refreshDisplay();
      },
      error: function( jqXHR, textStatus, errorThrown ) {
        alert( textStatus );
      }
    });
  }

  function parseType(type) {
    switch (type) {
    case "P0":
        return "P0-启动成功"
    case "P1":
        return "P1-启动最大次数失败"
    case "P2":
        return "P2-进程非手动退出"
    case "P3":
        return "P3-进程手动退出"
    case "A1":
        return "A1-进程资源超限"
    case "A2":
        return "A2-系统资源超限"
    default:
        return type
    }
  }

  $(document).ready(function() {
      $("form").submit(function(e){
        e.preventDefault();
        list_events();
      })
      list_events();
      // programs = [{"Uid":9303,"EventTime":1627393434425,"Type":"A2","Name":"OS","Desc":"Mem:\u003e52|56.8"},{"Uid":9304,"EventTime":1627393434534,"Type":"A1","Name":"testapp.jar","Desc":"CPU:\u003e100|223.3","LogName":"2021-07-27-21-43-54.534__A1__testapp.jar.log"},{"Uid":9305,"EventTime":1627393434571,"Type":"A1","Name":"testapp.jar","Desc":"Mem:\u003e6|6.9","LogName":"2021-07-27-21-43-54.571__A1__testapp.jar.log"},{"Uid":9306,"EventTime":1627393444424,"Type":"A2","Name":"OS","Desc":"Mem:\u003e52|56.8","LogName":"2021-07-27-21-44-04.424__A2__OS.log"},{"Uid":9307,"EventTime":1627393444502,"Type":"A1","Name":"testapp.jar","Desc":"CPU:\u003e100|221.9","LogName":"2021-07-27-21-44-04.502__A1__testapp.jar.log"},{"Uid":9308,"EventTime":1627393444526,"Type":"A1","Name":"testapp.jar","Desc":"Mem:\u003e6|6.9","LogName":"2021-07-27-21-44-04.526__A1__testapp.jar.log"},{"Uid":9309,"EventTime":1627393454425,"Type":"A2","Name":"OS","Desc":"Mem:\u003e52|56.8","LogName":"2021-07-27-21-44-14.425__A2__OS.log"},{"Uid":9310,"EventTime":1627393454491,"Type":"A1","Name":"testapp.jar","Desc":"CPU:\u003e100|226.7","LogName":"2021-07-27-21-44-14.491__A1__testapp.jar.log"},{"Uid":9311,"EventTime":1627393454523,"Type":"A1","Name":"testapp.jar","Desc":"Mem:\u003e6|6.9","LogName":"2021-07-27-21-44-14.523__A1__testapp.jar.log"},{"Uid":9312,"EventTime":1627393464433,"Type":"A2","Name":"OS","Desc":"Mem:\u003e52|56.8","LogName":"2021-07-27-21-44-24.433__A2__OS.log"},{"Uid":9313,"EventTime":1627393464487,"Type":"A1","Name":"testapp.jar","Desc":"CPU:\u003e100|221.2","LogName":"2021-07-27-21-44-24.487__A1__testapp.jar.log"},{"Uid":9314,"EventTime":1627393464522,"Type":"A1","Name":"testapp.jar","Desc":"Mem:\u003e6|6.9","LogName":"2021-07-27-21-44-24.522__A1__testapp.jar.log"},{"Uid":9315,"EventTime":1627393474424,"Type":"A2","Name":"OS","Desc":"Mem:\u003e52|56.8","LogName":"2021-07-27-21-44-34.424__A2__OS.log"},{"Uid":9316,"EventTime":1627393474515,"Type":"A1","Name":"testapp.jar","Desc":"CPU:\u003e100|219.5","LogName":"2021-07-27-21-44-34.515__A1__testapp.jar.log"},{"Uid":9317,"EventTime":1627393474560,"Type":"A1","Name":"testapp.jar","Desc":"Mem:\u003e6|6.9","LogName":"2021-07-27-21-44-34.560__A1__testapp.jar.log"},{"Uid":9318,"EventTime":1627393484424,"Type":"A2","Name":"OS","Desc":"Mem:\u003e52|56.8","LogName":"2021-07-27-21-44-44.424__A2__OS.log"},{"Uid":9319,"EventTime":1627393484475,"Type":"A1","Name":"testapp.jar","Desc":"CPU:\u003e100|224.3","LogName":"2021-07-27-21-44-44.475__A1__testapp.jar.log"},{"Uid":9320,"EventTime":1627393484500,"Type":"A1","Name":"testapp.jar","Desc":"Mem:\u003e6|6.9","LogName":"2021-07-27-21-44-44.500__A1__testapp.jar.log"}]
      // for(var i=0; i<programs.length; i++) {
      //   programs[i].EventTime = formatDate(programs[i].EventTime)
      //   programs[i].Type = parseType(programs[i].Type)
      // }
      // refreshDisplay();
  });
  </script>
  <body>
    <h3 class="text-center text-success">Buddhad(如来)守护程序</h3>
    <div class="container">
      <H4>事件（Event）列表</H4>
      <div class='row' style="margin-left: 0;">
        <form class="form-inline">
            <div class="form-group">
              <label for="Name">Name</label>
              <input type="text" class="form-control" id="Name" placeholder="testapp">
            </div>
            <div class="form-group" style="margin-left: .45rem!important;">
              <label for="Type">Type</label>
              <select class="form-control" id="Type">
                <option value=""></option>
                <option value="P2">P2-进程非手动退出</option>
                <option value="P1">P1-启动最大次数失败</option>
                <option value="P0">P0-启动成功</option>
                <option value="P3">P3-进程手动退出</option>
                <option value="A1">A1-进程资源超限</option>
                <option value="A2">A2-系统资源超限</option>
              </select>
            </div>
            <div class="form-group" style="margin-left: .45rem!important;">
              <label for="eventTimeStart">开始</label>
              <input type="text" class="form-control" id="eventTimeStart" placeholder="2021-7-27 14:8:10">
            </div>
            <div class="form-group" style="margin-left: .45rem!important;">
              <label for="eventTimeEnd">结束</label>
              <input type="text" class="form-control" id="eventTimeEnd" placeholder="2021-7-27 14:9">
            </div>
            <button type="submit" id="query_sub" class="btn btn-primary" style="margin-left: .45rem!important;">查询</button>
        </form>
      </div>
      <div class="table-responsive mt-3">          
       <table id="programs"
           data-toggle="table"
           data-click-to-select="true" >
           <thead>
               <th data-field="EventTime">EventTime</th>
               <th data-field="Name">Name</th>
               <th data-field="Type">Type</th>
               <th data-field="Desc">Description</th>
               <th data-field="action">Log</th>
           </thead>
       </table>
      </div>
    </div>


    <div id="myModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="my-modal-title">Confirmation</h4>
                </div>
                <div class="modal-body">
                    <p id="my-modal-message">Do you want to save changes you made to document before closing?</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" data-dismiss="modal" id='my-modal-cancel-btn'>Cancel</button>
                    <button type="button" class="btn btn-primary" data-dismiss="modal" id='my-modal-confirm-btn'>Shutdown</button>
                </div>
            </div>
        </div>
    </div>


  </body>

</html>
